<php>
	$this->display(tpl('head',true));
</php> 
<style>
	.layui-table thead tr th{
		text-align: center;
	}
	.w-100{
		width: 100%;
	}
	.button{
		float: left;
		width: 100%;
		text-align: center;
		margin-bottom: 20px;
	}
	.button button{
		background-color: #6666FF;
		border: 0;
		color: #fff;
		min-width: 100px;
		height: 35px;
		font-size: 1.2em;
		cursor:pointer;
		padding: 0 10px;
		margin-right: 10px;
	}
	.button button:hover{
		background-color: #6756EA;
	}
	.payment .layui-unselect {
		border: 1px solid #ddd;
		padding: 0 10px;
	}
	.payment .layui-unselect i {
		display: none;
	}
	.payment .layui-form-radioed {
		border: 1px solid #c81623;
		background: url('/public/global/images/dui.png') no-repeat bottom right;
	}
	.payment .layui-form-radio{
		margin-top: 0;
	}
	.poundage{
		line-height: 35px;
	}
	.qrcode{
		display:none;
	}
	.qrcode span{
		float: left;
		width: 100%;
		text-align: center;
	}
	.qrcode span:last-child{
		margin-top: 20px;
	}
	.qrcode a{
		border-radius: 5px;
		border: 1px solid #009688;
		width: 100px;
		padding: 10px;
	}
</style>
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
  	<legend>广告购买</legend>
</fieldset>
<form class="layui-form">
	<table class="layui-table" cellspacing="0" cellpadding="0" border="0" >
		
		<tbody>
			<tr>
				<td align="center" width="100">广告名称</td>
				<td>{{$data['name']}}</td>
			</tr>
			<tr>
				<td align="center" width="100">广告说明</td>
				<td>{{$data['msg']}}</td>
			</tr>
			<tr>
				<td align="center" width="100">广告类型</td>
				<td>
					<php> 
						if($data['ad_type']=="images"){
							echo "图片类型";
						}elseif($row['ad_type']=="text"){
							echo "文字类型";
						}else{
							echo "代码类型";
						}
						
					</php>
				</td>
			</tr>
			<tr>
				<td align="center" width="100">广告时间选择</td>
				<td>
					<div style="width: 30%;">
						<select name="time" lay-filter="time">
							<option value="">请选择购买天数</option>
							<foreach array="data['price']['time']">
								<option value="{{$key}}">{{$row}}天</option>
							</foreach>
						</select>
					</div>
				</td>
			</tr>
			<tr>
				<td align="center" width="100">广告价格</td>
				<td style="font-size: 18px;font-weight: bold;color: red;">
					<span id="price" style="color: green;margin: 0 10px;">0</span>元
				</td>
			</tr>
			<if condition="C('advertisement|setting.is_integral') && $data['integral']">
			<tr>
				<td align="center" width="100">积分抵扣</td>
				<td>
					<input id="integral" style="width: 100px;" class="layui-input" type="integral" name="integral" value="0" />
					<p>需要抵扣的数量，{{C('advertisement|setting.integral')}}积分兑换一块钱，最大抵扣数量{{$data['integral']}},您现有积分为:{{C('MYDB.integral')}}</p>
				</td>
			</tr>
			</if>
			<tr>
				<td colspan="2">
					<input type="hidden" name="tag" id="" value="{{$_GET['tag']}}" />
					<button type="button" class="layui-btn" lay-submit lay-filter="post">确定下单</button>
				</td>
			</tr>
		</tbody>
	</table>
</form>
<div class="pay" id="pay" style="display: none;">
	<form class="layui-form" action="" method="post">
		<div class="content">
			<div class="w-100 mt10 left" style="line-height: 35px;">
				<label class="left pl10 tar">订单号：</label>
				<div class="payment" id="transaction_code"></div>
			</div>
			<div class="w-100 mt10 left" style="line-height: 35px;">
				<label class="left pl10 tar">支付金额：</label>
				<div class="payment" style="font-weight: bold;color: red;">
					<span  id="rmb"></span> 元
				</div>
			</div>
			<div class="w-100 mt10 left" style="line-height: 35px;">
				<label class="left pl10 tar">支付方式：</label>
				<div class="payment">
					<if condition="C('payment|payment_list')">
						<foreach array='C("payment|payment_list")' f="1">
							<php> $i++;$checked='';if($i==1){$checked='checked';$_payment_name=$row['name'];}</php>
							<input {{$checked}} lay-filter="payment" title="{{$row[name]}}" type="radio" name="payment"  value="{{$key}}"/>
						</foreach>
					</if>
					<input lay-filter="rmb" title="余额" type="radio" name="payment"  value="rmb"/>
				</div>
			</div>
			<div class="button mt10">
				<input type="hidden" name="id" id="pid" />
				<button type="button" lay-filter="postpaydata" lay-submit>确定</button>
			</div>
		</div>
	</form>
</div>
<div class="qrcode" id="qrcode">
	<span><img src="" ></span>
	<span><a id="qrcode_url" href="" target="_blank">直接电脑支付</a></span>
</div>
<script>
	var price= {{json_encode($data['price']['price'])}};
	var integral={{intval($data['integral'])}};
	var maxintegral={{$data['integral']}};
	var integral_exchange_rmb={{intval(C('advertisement|setting.integral'))}};
	var myintegral={{C('MYDB.integral')}};
	var need_price=0;
	var integral_exchang=0;
	$("#integral").focusout(function() {
		integral_exchang=this.value;
		calculation();
	});
	
	
	function calculation(){
		if(integral_exchang > maxintegral){
			layer.msg('最大积分抵扣不能大于'+maxintegral);
			return;
		}
		if(integral_exchang > myintegral){
			layer.msg('抵扣最分不能大于您现有积分，您现有积分为:'+myintegral);
			return;
		}
		if(integral_exchang && integral_exchang != 0){
			var _rmb=integral_exchang / integral_exchange_rmb;
			$('#price').html(need_price.toString() +'<span style="font-size:12px;font-weight:0;color:#666">(总金额)</span> - '+_rmb.toString()+'<span style="font-size:12px;font-weight:0;color:#666">(积分兑换人民币)</span> = '+ (need_price - _rmb) +'<span style="font-size:12px;font-weight:0;color:#666">(需支付的金额)</span>') ;
		}else{
			$('#price').html(need_price);
		}
		
		
	}
	
	
	function payok(id){
		layer.confirm('您额广告位已经购买成功是否现在去设置', {icon: 3, title:'广告位设置提醒'}, function(index){
			window.location.href="{{user_plugin_url}}/index/edit/id/"+id;
			layer.closeAll();
		}, function(index, layero){ 
		  layer.closeAll();
		  window.location.href="{{user_plugin_url}}/index/mylist";
		  return false; 
		});
	}
	
	function ajax_order_state(id) {
		$.ajax({
			type: "POST",
			url:" {{user_plugin_url}}/index/test_pay_state/id/"+id+'{{C('HTML')}}?'+Date.parse(new Date()),
			dataType:'html',
			success: function(res){
				if(res != 0){
					clearInterval(set_Interval);
					payok(res);
				}
			}
		})
	}
	
	var set_Interval;
	var payment='{{$_payment_name}}';
	layui.use(['table','form'], function() {
		var form=layui.form;
		form.on('select(time)', function(data){
			need_price=price[data.value];
			calculation()
		});
		form.on('radio(payment)', function(data){
			payment=data.elem.title; //得到radio原始DOM对象
		});  
		form.on('submit(postpaydata)', function(data){
			if(!data.field.payment){
				layer.msg('请选择一个支付方式');
				return;
			}
			if(data.field.payment=='rmb'){
				layer.prompt({
					formType: 1,
					title: '请输入支付密码',
				}, function(value, index, elem){
					data.field.rmbpassword=value;
					$.post("{{user_plugin_url}}/index/pay_order",data.field, function(data){
						layer.msg(data.text);
						if(data.code==0){
							payok(data.id);
						}
					},'json');
				});
			}else{
				
				$.post("{{user_plugin_url}}/index/pay_order",data.field, function(res){
					if(res.code==0){
						layer.open({
							title :payment+'扫一扫支付',
							area :['30%','300px'],
							type: 1,
							content: $('#qrcode'),
							cancel: function(index, layero){ 
								clearInterval(set_Interval);
								layer.close(index);
							  return false; 
							}
						});
						$('#qrcode_url').attr('href','{{P_WEBURL}}/index/pay/id/'+data.field.id+'{{C('html')}}');
						$('#qrcode img').attr('src','{{user_plugin_url}}/index/qrcode/id/'+data.field.id+'{{C('html')}}');
						set_Interval = setInterval(function(){
							ajax_order_state(data.field.id);
						},5000);
					}
				},'json');
				
			}
			return;
		});
		
		
		
		
		form.on('submit(post)', function(data){
			
			if(data.field.time ==''){
				layer.msg('请选择一个广告时间');
				return;
			}
			
			console.log(data)
			$.post("{{user_plugin_url}}/index/create_order",data.field, function(data){
				layer.msg(data.text);
				if(data.code==0){
					$('#transaction_code').html(data.data.transaction_code);
					$('#rmb').html(data.data.rmb);
					$('#pid').val(data.data.id);
					layer.open({
						title: '广告订单支付',
						type:1,
						content: $('#pay')
					});  
				}
			   console.log(data)
			},'json');
			
		  console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
		  return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
		});
	});
</script>
<php>
	$this->display(tpl('foot',true));
</php>
